<template>
	<view>
		<view class="head">
			<view class="more">
				<view class="list" v-for="(item,index) in name" @click="select(index)">
					<view class="className">{{item}}</view>
					<view class="line" v-if="state==index"></view>
				</view>
			</view>
		</view>
		<template v-for="(item,index) in list">
			<view class="orderList" v-if="2==item.serv_enum" @click="detail(item.serv_order_id)">
				<view class="headCon">
					<view class="headName">{{item.serv_name}}服务</view>
					<view class="state" >{{item.status_text}}</view>
					
				</view>
				<view class="address">{{item.destination}}</view>
				<view class="time">{{item.stat_1_time}}</view>
				<view class="foot" v-if="2==item.status">
					<view class="operate" @click.stop="cancel_order(item.serv_order_id)" v-if="item.status==2">取消接单</view>
					<view class="operate redcolors" @click.stop="callphone(item.contact_phone)" v-if="item.status==2">联系雇主</view>
					<view class="operate redcolors" @click.stop="do_set_code(item.serv_order_id)"  v-if="item.status==2">开始服务</view>
				</view>
			</view>
			<view class="orderList" v-else @click="detail(item.serv_order_id)">
				<view class="headConTwo">
					<view class="addressCon">
						<view class="linesolid"></view>
						<view class="addressname">
							<view class="drop"></view>
							<view class="detail">{{item.departure}}</view>
						</view>
						<view class="addressname mtop">
							<view class="drop"></view>
							<view class="detail">{{item.destination}}</view>
						</view>
					</view>
					<view class="state">{{item.status_text}}</view>
					
				</view>
				<view class="time">{{item.stat_1_time}}</view>
				<view class="foot" v-if="2==item.status">
					<view class="operate" @click.stop="cancel_order(item.serv_order_id)" v-if="item.status==2">取消接单</view>
					<view class="operate redcolors" @click.stop="callphone(item.contact_phone)" v-if="item.status==2">联系雇主</view>
					<view class="operate redcolors" @click.stop="do_set_code(item.serv_order_id)" v-if="item.status==2">开始服务</view>
				</view>
			</view>
		</template>
		
		<view class="blackBox" v-if="set_code">
			<view class="whiteBox">
				<view class="alertTitle">请输入核销码(由雇主提供)</view>
				<view class="alertCon">
					<input type="text" placeholder="输入核销码" placeholder-class="mo" v-model="check_code" />
				</view>
				<view class="alertTwo">
					<view class="close" @click="do_set_code(0)">取消</view>
					<view class="ok" @click="begin_order()">确认</view>
				</view>
			</view>
			<view class="closeimg">
				<image src="../../../static/images/cc/close2.png" mode="widthFix" @click="do_set_code(0)"></image>
			</view>
		</view>
		<view class="null"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				state: 0,
				name: ['全部', '已接单', '进行中', '已完成'],
				stateArr:["0","2","3","4,5"],
				aList: [{
					state: '1'
				}, {
					state: '2'
				}, {
					state: '3'
				}],
			
				list:[],
				page:0,
				loading:false,
				set_code:false,
				check_code_order:0,
				check_code:'',
			}
		},
		onLoad(){
			this.page=0;
			this.listOrder();
		},
		onPullDownRefresh() {
			this.page=0;
			this.listOrder();
		},
		onReachBottom() {
			this.listOrder();
		},
		methods: {
			do_set_code(order_id) {
				this.check_code='';
				this.check_code_order = order_id;
				this.set_code = !this.set_code
			},
			async begin_order(){
				var that = this;
				await uni.request({
					url: '/dispatch/server/beginOrder',
					data: {
						order_id:this.check_code_order,
						check_code:this.check_code,
					},
					method:"POST",
					success: res => {
						this.do_set_code(0);
						console.log("更新列表")
						setTimeout(()=>{
							that.page = 0;
							that.listOrder();
						},500);
					},
					complete:()=>{
						// uni.hideLoading();
						// that.loading = false;
					}
				});	
			},
			async cancel_order(order_id){
				var that = this;
				if(that.loading){
					return;
				}
				that.loading = true;
				uni.showLoading({
					title: '取消中'
				});
				await uni.request({
					url: '/dispatch/server/cancelOrder',
					data: {
						order_id:order_id
					},
					method:"POST",
					success: res => {
						console.log("更新列表")
						setTimeout(()=>{
								that.page = 0;
								that.listOrder();
						},500);
						
					},
					complete:()=>{
						setTimeout(()=>{
							uni.hideLoading();
							that.loading = false;
						},100);
					}
				});	
			},
			callphone(phone){
				uni.makePhoneCall({
					phoneNumber: phone //仅为示例
				});
			},
			async listOrder(){
				if(this.loading){
					return;
				}
				this.loading = true;
				uni.showLoading({
					title: '加载中'
				});
				if(0==this.page){
					this.list = [];
				}
				this.page++;
				// 获取列表
				await uni.request({
					url: '/dispatch/server/getOrderList',
					data: {
						state:this.stateArr[this.state],
						page:this.page
					},
					method:"POST",
					success: res => {
						this.list = this.list.concat(res.data);
						console.log(res);
					},
					complete:()=>{
						setTimeout(()=>{
							uni.stopPullDownRefresh();
							uni.hideLoading();
							this.loading = false;	
						},100);
					}
				});	
			},
			select(e) {
				this.state = e
				this.page = 0;
				this.listOrder();
			},
			detail(order_id) {
				uni.navigateTo({
					url: '/pages/dispatch/myOrderDetail/myOrderDetail?order_id='+order_id
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f7f7f7;
	}

	.head {
		width: 750rpx;
		background-color: #fff;
		display: flex;
		justify-content: center;
		flex-wrap: wrap;
		margin-bottom: 20rpx;
	}

	.titles {
		width: 340rpx;
		height: 62rpx;
		border-radius: 50rpx;
		background: rgba(232, 55, 41, 0.1);
		display: flex;
		margin-top: 36rpx;
		justify-content: center;
		align-items: center;
	}

	.names {
		width: 162rpx;
		height: 54rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 50rpx;
		font-size: 26rpx;
		color: #84BD00;
	}

	.selects {
		background-color: #84BD00;
		color: #fff;
	}

	.more {
		width: 100%;
		display: flex;
		justify-content: space-between;
		padding: 20rpx 0;
	}

	.list {
		width: 20%;
		display: flex;
		justify-content: center;
		flex-wrap: wrap;
	}

	.className {
		width: 100%;
		font-size: 30rpx;
		color: #3D3D3D;
		text-align: center;
	}

	.line {
		width: 52rpx;
		height: 8rpx;
		background-color: #84BD00;
		border-radius: 50rpx;
		margin-top: 4rpx;
	}

	.orderList {
		width: 690rpx;
		background-color: #fff;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
		margin-left: 30rpx;
		padding-top: 46rpx;
		padding-bottom: 36rpx;
	}

	.headCon {
		width: 630rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-left: 30rpx;
	}

	.headConTwo {
		width: 630rpx;
		display: flex;
		justify-content: space-between;
		margin-left: 30rpx;
		padding-bottom: 20rpx;
	}

	.headName {
		font-size: 36rpx;
		color: #3D3D3D;
	}

	.state {
		font-size: 26rpx;
		color: #84BD00;
	}

	.address {
		width: 630rpx;
		margin-left: 30rpx;
		color: #999999;
		font-size: 30rpx;
		padding: 20rpx 0;
	}

	.foot {
		width: 630rpx;
		margin-left: 30rpx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		margin-top: 38rpx;
	}

	.time {
		font-size: 26rpx;
		color: #999999;
		margin-left: 30rpx;
	}

	.operate {
		width: 162rpx;
		height: 54rpx;
		border: 1px solid #DBDBDB;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 26rpx;
		color: #333333;
		border-radius: 50rpx;
	}

	.drop {
		width: 14rpx;
		height: 14rpx;
		background-color: #D8D8D8;
		border-radius: 50%;
		margin-right: 20rpx;
	}

	.addressCon {
		width: 500rpx;
		position: relative;
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}

	.linesolid {
		position: absolute;
		left: 7rpx;
		border-left: 1px dashed #D8D8D8;
		height: 60%;
	}

	.addressname {
		font-size: 30rpx;
		color: #333333;
		display: flex;
		align-items: center;
	}

	.detail {
		width: 466rpx;
	}

	.mtop {
		margin-top: 20rpx;
	}
	
	.null{
		width: 750rpx;
		height: 20rpx;
	}
	.redcolors{
		margin-left: 20rpx;
		border: 1px solid #84BD00;
		color: #84BD00;
	}
	
	.blackBox {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		background: rgba(0, 0, 0, 0.7);
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		align-items: center;
		align-content: center;
	
		.whiteBox {
			width: 540rpx;
			background-color: #ffffff;
			border-radius: 20rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: center;
	
			.alertTitle {
				width: 100%;
				text-align: center;
				font-size: 30rpx;
				color: #3D3D3D;
				padding: 40rpx 0;
			}
	
			.alertCon {
				width: 470rpx;
				height: 70rpx;
				border-radius: 10rpx;
				background-color: #F6F6F6;
	
				input {
					width: 422rpx;
					margin-left: 24rpx;
					height: 70rpx;
					font-size: 26rpx;
				}
	
				.mo {
					color: #999999;
				}
			}
	
			.alertTwo {
				width: 100%;
				display: flex;
				border-top: 1px solid #D8D8D8;
				margin-top: 42rpx;
	
				.close {
					width: 50%;
					height: 96rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					box-sizing: border-box;
					border-right: 1px solid #D8D8D8;
				}
	
				.ok {
					width: 50%;
					height: 96rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					box-sizing: border-box;
					color: #CA281C;
				}
			}
		}
	
		.closeimg {
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			justify-content: center;
			margin-top: 55rpx;
	
			image {
				width: 74rpx;
				height: 74rpx;
			}
		}
	}
	
</style>